package cn.web.tunnelrobot.controller;

import cn.web.tunnelrobot.dto.HardwareTestInfoDTO;
import cn.web.tunnelrobot.dto.Project1;
import cn.web.tunnelrobot.dto.RealTimeData;
import cn.web.tunnelrobot.service.HardwareTestInfoService;
import cn.web.tunnelrobot.service.HomePageService;
import cn.web.tunnelrobot.util.Constants;
import cn.web.tunnelrobot.util.MileageConversion;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.ResponseEntity;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/homePage")
@Api(tags = "主页")
@ApiSort(value = 4)
public class HomePageController {
    @Autowired
    private HomePageService homePageService;
    @Autowired
    private HardwareTestInfoService hardwareTestInfoService;

    /**
     * 获取隧道数据（隧道长度，隧道各状态数据）
     * @param pageNo
     * @param prId
     * @return
     */
    @GetMapping("/getTunnel")
    @ResponseBody
    @ApiOperation(value = "pageNo，项目id",notes = "获取隧道数据（隧道长度，隧道各状态数据）")
    public HashMap<String,Object> getTunnel(@RequestParam(required = false) Integer pageNo,
                                            @RequestParam Integer prId){
        return homePageService.getTunnel(pageNo,prId);
    }

    @GetMapping("/getTunnelInformation")
    @ResponseBody
    @ApiOperation(value = "项目id",notes = "获取隧道相关信息（隧道长度、施工状态数据、模型放置位置、初始里程、尾部里程等）")
    public HashMap<String,Object> getTunnelInformation(@RequestParam Integer prId){
        return homePageService.getTunnelInformation(prId);
    }

 /*   *//*
     * 隧道模型施工进度
     * imageinfo表
     * @param prId
     * @return
     *//*
    @GetMapping("/getConstructionProgress")
    @ResponseBody
    @ApiOperation(value = "项目id",notes = "隧道模型施工进度imageinfo表")
    public HashMap<String,Object> getConstructionProgress(@RequestParam Integer prId){
        return homePageService.getConstructionProgress(prId);
    }*/

    /**
     * 隧道项目完成进度展示
     * project表
     * 二期
     * gbq
     * */
    @GetMapping("/getProjectProgress")
    @ResponseBody
    @ApiOperation(value = "项目id",notes = "隧道模型施工进度project表")
    public HashMap<String,Object> getProjectProgress(@RequestParam Integer prId){
        return homePageService.getProjectProgressById(prId);
    }
    /**
     * 修改隧道项目进度
     * project表
     * 二期
     * gbq
     * */
    @GetMapping("/updateProjectProgress")
    @ResponseBody
    @ApiOperation(value = "项目id",notes = "修改隧道项目进度")
    public HashMap<String,Object> updateProjectProgress(@RequestParam(required = true,name="prId") Integer prId,
                                                        @RequestParam(required = true,name="隧道初始里程") Integer  d1,
                                                        @RequestParam(required = true,name="隧道初始里程1") Integer   d1_1,
                                                        @RequestParam(required = true,name="隧道尾部里程") Integer   d2,
                                                        @RequestParam(required = true,name="隧道尾部里程1") Integer   d2_1,
                                                        @RequestParam(required = true,name="开挖初始里程") Integer   d7,
                                                        @RequestParam(required = true,name="开挖初始里程1") Integer   d7_1,
                                                        @RequestParam(required = true,name="初支初始里程") Integer   d5,
                                                        @RequestParam(required = true,name="初支初始里程1") Integer   d5_1,
                                                        @RequestParam(required = true,name="二衬初始里程") Integer   d3,
                                                        @RequestParam(required = true,name="二衬初始里程1") Integer   d3_1,
                                                        @RequestParam(required = true,name="开挖尾部里程") Integer   d8,
                                                        @RequestParam(required = true,name="开挖尾部里程1") Integer   d8_1,
                                                        @RequestParam(required = true,name="初支尾部里程") Integer   d6,
                                                        @RequestParam(required = true,name="初支尾部里程1") Integer   d6_1,
                                                        @RequestParam(required = true,name="二衬尾部里程") Integer   d4,
                                                        @RequestParam(required = true,name="初二衬尾部里程1") Integer   d4_1,
                                                        @RequestParam(required = true,name="开挖计划初始里程") Integer   d13,
                                                        @RequestParam(required = true,name="开挖计划初始里程1") Integer   d13_1,
                                                        @RequestParam(required = true,name="初支计划初始里程") Integer   d11,
                                                        @RequestParam(required = true,name="初支计划初始里程1") Integer   d11_1,
                                                        @RequestParam(required = true,name="二衬计划初始里程") Integer   d9,
                                                        @RequestParam(required = true,name="二衬计划初始里程1") Integer   d9_1
                                                        ){
        Project1 p=new Project1();
        p.setPrId(prId);
        p.setD1(d1);
        p.setD1_1(d1_1);
        p.setD2(d2);
        p.setD2_1(d2_1);
        p.setD3(d3);
        p.setD3_1(d3_1);
        p.setD4(d4);
        p.setD4_1(d4_1);
        p.setD5(d5);
        p.setD5_1(d5_1);
        p.setD6(d6);
        p.setD6_1(d6_1);
        p.setD7(d7);
        p.setD7_1(d7_1);
        p.setD8(d8);
        p.setD8_1(d8_1);
        p.setD9(d9);
        p.setD9_1(d9_1);
        p.setD11(d11);
        p.setD11_1(d11_1);
        p.setD13(d13);
        p.setD13_1(d13_1);
        return homePageService.updateProjectProgress(p);
    }

    /**
     * 首页隧道数据查询
     * @param prId
     * @param imMileag
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getHardwareTestLastInfoByPrIdAndMileage")
    @ResponseBody
    @ApiOperation(value = "项目id，pageNo，pageSize",notes = "首页隧道数据查询")
    public HashMap<String,Object> getHardwareTestLastInfoByPrIdAndMileage(@RequestParam Integer prId, @RequestParam String imMileag,
                                                                          @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                                          @RequestParam(value = "pageSize",defaultValue = "20") Integer pageSize){
        return hardwareTestInfoService.getHardwareTestInfoByPrIdAndMileage(prId,imMileag,pageNo,pageSize);
    }

    /**
     * 警告记录轮播数据
     * warningrecord表
     * @return
     */
    @GetMapping("/homePageShuffling")
    @ResponseBody
    public HashMap<String,Object> homePageShuffling(){
        return homePageService.homePageShuffling();
    }

    /**
     * 获取氧气浓度
     * @return
     */
    @GetMapping("/oxygenConcentrations")
    @ResponseBody
    public HashMap<String,Object> oxygenConcentrations(){
        HashMap<String,Object> map = new HashMap<>();
        RealTimeData realTimeData = Constants.realTimeData;
        if (realTimeData.getOxygenConcentrations()== null){
            map.put("oxygenConcentrations",0);
        }else {
            map.put("oxygenConcentrations",realTimeData.getOxygenConcentrations());
        }
        map.put("code",0);
        return map;
    }

    /**
     * 悬浮（瓦斯浓度）
     * @return
     */
    @GetMapping("/gasDensity")
    @ResponseBody
    public HashMap<String,Object> gasDensity(){
        HashMap<String,Object> map = new HashMap<>();
        RealTimeData realTimeData = Constants.realTimeData;
        if (realTimeData.getGasDensity() == null ){
            map.put("gasDensity",0);
        }else {
            map.put("gasDensity",realTimeData.getGasDensity());
        }
        map.put("code",0);
        return map;
    }

    /**
     * 模型-隧道模型每一段的沉降收敛列表
     * @param prId
     * @param htMileage
     * @param htDailySettl1
     * @param htDailySettl2
     * @param htDailyConvergence1
     * @param htDailyConvergence2
     * @param time1
     * @param time2
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/TunnelInspection")
    @ResponseBody
    @ApiOperation(value = "项目id，里程，每日沉降值1，每日沉降值2，每日收敛值1，每日收敛值2，时间1，时间2，pageNo，pageSize",notes = "隧道模型每一段的沉降收敛列表")
    public HashMap<String,Object> TunnelInspection(@RequestParam Integer prId,
                                                  @RequestParam(required = false) Integer htMileage,
                                                  @RequestParam(required = false) Float htDailySettl1,
                                                   @RequestParam(required = false) Float htDailySettl2,
                                                  @RequestParam(required = false) Float htDailyConvergence1,
                                                   @RequestParam(required = false) Float htDailyConvergence2,
                                                  @RequestParam(required = false) String time1,
                                                  @RequestParam(required = false) String time2,
                                                  @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                  @RequestParam(value = "pageSize",defaultValue = "20") Integer pageSize){
        return homePageService.TunnelInspection(prId,htMileage,htDailySettl1,htDailySettl2,htDailyConvergence1,htDailyConvergence2,time1,time2,pageNo,pageSize,0);
    }

    /**
     * 导出 Excel 表格
     * @return
     */
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportData() {
        // 1.这一步就是查询你要导出的数据
        Integer prId = 1;
        Integer htMileage = null;
        Float htDailySettl1 = null;
        Float htDailySettl2 = null;
        Float htDailyConvergence1 = null;
        Float htDailyConvergence2 = null;
        String time1 = null;
        String time2 = null;
        Integer pageNo = null;
        Integer pageSize = null;
        Integer export = 1;
        HashMap<String, Object> map = new HashMap<>();
        map =  homePageService.TunnelInspection(prId,htMileage,htDailySettl1,htDailySettl2,htDailyConvergence1,htDailyConvergence2,time1,time2,pageNo,pageSize,export);
        List<HardwareTestInfoDTO> list = new ArrayList<>();
        list = (List<HardwareTestInfoDTO>) map.get("data");
        // 2.创建一个 POIUtils 工具类进行导出操作
        return homePageService.employee2Excel(list);
    }

    /**
     * 悬浮-沉降收敛列表
     * 变形检测页面
     * hardwaretestinfo表
     * @param prId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/SettlAndConvergence")
    @ResponseBody
    @ApiOperation(value = "变形检测页面（二期新增，单独页面）",notes = "悬浮-沉降收敛列表")
    public HashMap<String,Object> SettlAndConvergence(@RequestParam Integer prId,
                                                      @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                                      @RequestParam(value = "pageSize",defaultValue = "20") Integer pageSize){
        return homePageService.SettlAndConvergence(prId,pageNo,pageSize);
    }

    /**
     * 获取机器人位置
     * @return
     */
    @GetMapping("/robotPosition")
    @ResponseBody
    public HashMap<String,Object> robotPosition(){
        HashMap<String,Object> map = new HashMap<>();
        Integer robotCurrentPosition = Constants.robotCurrentPosition;
        map.put("code",0);
        map.put("position", MileageConversion.mileage(robotCurrentPosition));
        return map;
    }

    /**
     * 项目简介
     * @return
     */
    @GetMapping("/getProjectIntro")
    @ResponseBody
    @ApiOperation(value = "无",notes = "项目简介")
    public HashMap<String,Object> getProjectIntro(){
        return homePageService.getProjectIntro();
    }

    @GetMapping("/getFive" )
    @ResponseBody
    public HashMap<String,Object> getFive(@RequestParam Integer type){
        return homePageService.getFive(type);
    }
}
